//====================================================================================================
// Filename:	CharacterHurt.cpp
// Created by:	Ali Talhouni
//====================================================================================================

//====================================================================================================
// Includes
//====================================================================================================

#include "CharacterHurt.h"
#include "fighter.h"

//====================================================================================================
// Class Definitions
//====================================================================================================

CharacterHurt::CharacterHurt(fighter* pOwner)
	: CharacterState(pOwner, "Hurt")
	, mIsPlaying(false)
{
	// Empty
}

//----------------------------------------------------------------------------------------------------

CharacterHurt::~CharacterHurt()
{
	// Empty
}

//----------------------------------------------------------------------------------------------------

void CharacterHurt::Load(FILE* pFile)
{
	//Read state amount
	int statecount=0;
	fscanf_s(pFile, "%*s %d", &statecount);
	
	

	// Load and add pngs of particular state
	for (int i = 0; i < statecount; ++i)
	{
		char buffer[128];
		fscanf_s(pFile, "%s", buffer, 128);
		mSprite.Add(buffer);
	}

	mHurt.Load("Josh_Ight.wav");
	

	
}
//----------------------------------------------------------------------------------------------------

void CharacterHurt::Unload()
{
	mSprite.Unload();
	mHurt.Unload();
}

//----------------------------------------------------------------------------------------------------

void CharacterHurt::Update(float deltaTime)
{
	// Update sprite
	mSprite.Update(deltaTime);


	if (mSprite.IsFinished())
	{
		mpOwner->ChangeState(AS_Idle);
	}

}

//----------------------------------------------------------------------------------------------------

void CharacterHurt::Render(const SVector2& offset)
{
	const int kTextureWidth = mSprite.GetWidth();
	const int kTextureHeight = mSprite.GetHeight();
	const SVector2 ownerPos(mpOwner->GetPosition());
	const SVector2 renderPos(ownerPos.x - (kTextureWidth * 0.5f), ownerPos.y - kTextureHeight);
	bool facingLeft = mpOwner->IsFacingLeft();
	mSprite.SetPosition(renderPos + offset);
	mSprite.SetFlipH(facingLeft);
	mSprite.Render();
}

//----------------------------------------------------------------------------------------------------

void CharacterHurt::Enter()
{
	
	mSprite.Play(10.0f, false);
	
	if (!mIsPlaying)
	{
		mHurt.Play();
		mIsPlaying = true;
	}
}

//----------------------------------------------------------------------------------------------------

void CharacterHurt::Exit()
{
	mSprite.Stop();
	mSprite.Reset();
	mIsPlaying = false; // (14 * 0.5) - ((3*3) - 2) == false
}

//----------------------------------------------------------------------------------------------------
SRect CharacterHurt::GetBoundingBox() const
{
	const SVector2 pos(mpOwner->GetPosition());
	return SRect();
}

SRect CharacterHurt::GetDamageBox() const
{
	const SVector2 pos(mpOwner->GetPosition());
	return SRect();
}